/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.spring.provider.plugin.impl;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;

import com.genia.toolbox.basics.exception.technical.TechnicalException;
import com.genia.toolbox.basics.exception.technical.TechnicalFileNotFoundException;
import com.genia.toolbox.spring.provider.plugin.SpringProviderPlugin;

/**
 * Defaut implementation of <code>SpringProviderPlugin</code>.
 * 
 * @see com.genia.toolbox.spring.provider.plugin.SpringProviderPlugin
 */
public abstract class AbstractSimpleSpringProviderPlugin
    implements SpringProviderPlugin
{

  /**
   * the config files to add to the spring configuration file.
   */
  private transient final String[] xmlConfigFileResources;



  /**
   * Constructor.
   * 
   * @param xmlConfigFileResources
   *          the config files to add to the spring configuration file
   */
  protected AbstractSimpleSpringProviderPlugin(final String... xmlConfigFileResources)
  {
    this.xmlConfigFileResources = xmlConfigFileResources;
  }



  /**
   * Return a {@link Collection} of {@link InputStream} that contains Spring
   * config files.
   * 
   * @return a {@link Collection} of {@link InputStream} that contains Spring
   *         config files
   * @throws TechnicalException
   *           when a configuration file does not exists
   */
  public Collection<InputStream> getConfigurations()
      throws TechnicalException
  {
    final InputStream[] res = new InputStream[xmlConfigFileResources.length];
    for (int index = 0; index < res.length; index++) {
      res[index] = getClass().getResourceAsStream(xmlConfigFileResources[index]);
      if (res[index] == null) {
        throw new TechnicalFileNotFoundException(new FileNotFoundException("Config file does not exists: " + xmlConfigFileResources[index]));
      }
    }
    return Arrays.asList(res);
  }

}
